﻿Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class Split
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Faz com que ao sair do aplicativo, limpe o buffer e não podendo entrar na página através do butão voltar do Browser.
        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        Try
            'If Not Session("Logged") Then
            '    Session("ShowSessionExpired") = 1 ' Show lblSessionExpired in Login.aspx
            '    Response.Redirect("Login.aspx")
            'End If

            If Not Page.IsPostBack Then

                ' Header
                lblModulo.Text = "Administrador"
                lblModuloItem.Text = "Split"

                btnIncluir.Enabled = False
                btnAlterar.Enabled = False
                btnExcluir.Enabled = False
                LoadHolding(True)

                FillDDL()

                If (CarregaGVW() = True) Then
                    btnIncluir.Enabled = True
                End If

            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Private Function LoadHolding(ByVal strConn As String) As Boolean

        LoadHolding = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String = "SELECT CodigoDaHolding, NomeDaHolding" & _
                         " FROM Holding;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                If (theSqlDataReader.HasRows = True) Then

                    ' Loop through all the rows in the DataTableReader.
                    Do While theSqlDataReader.Read()
                        lblHolding.Text = theSqlDataReader(1).ToString
                        lblHolding.ToolTip = theSqlDataReader(0).ToString
                    Loop

                    LoadHolding = True

                Else
                    lblMsg.Text = "Holding não cadastrada no Sistema."
                End If

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    Private Sub FillDDL()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDaCia, CodigoDaCia + ' - ' + NomeDaCia AS Cia" & _
                     " FROM Companhia" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " ORDER BY CodigoDaCia;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlCompanhia.DataTextField = "Cia"
                ddlCompanhia.DataValueField = "CodigoDaCia"
                ddlCompanhia.DataSource = mydatatable
                ddlCompanhia.DataBind()

                ddlCompanhia.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

 
    Private Function CarregaGVW() As Boolean

        Dim theQueryString As String = ""
        Dim strWhere As String = ""

        CarregaGVW = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Try
                theQueryString = " SELECT CodigoDaCia, CodigoSplit, DescSplit" & _
                                 " FROM Split"

                If Not (ddlCompanhia.Text = "") Then
                    strWhere = " CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'"
                End If

                'If Not (Trim(txtDDE.Text) = "") Then
                '    If (strWhere = "") Then
                '        strWhere = " DDE LIKE '%" & txtDDE.Text & "%'"
                '    Else
                '        strWhere = strWhere & _
                '                   " AND DDE LIKE '%" & txtDDE.Text & "%'"
                '    End If
                'End If

                If Not (strWhere = "") Then
                    theQueryString = theQueryString & _
                                     " WHERE " & strWhere
                End If

                theQueryString = theQueryString & _
                                 " ORDER BY CodigoDaCia, CodigoSplit DESC;"

                Dim mydatatable As New DataTable
                Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()

                theSqlCommand.CommandText = theQueryString
                theSqlConnection.Open()

                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)

                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                theSqlConnection.Close()

                CarregaGVW = True

            Catch ex As Exception
                theSqlConnection.Close()
                lblMsg.Text = ex.Message
            End Try

        End Using

    End Function

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            CarregaGVW()
            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        Dim strSituacaoDDE As String = ""

        Try
            BtnConsultar.Enabled = True

            Session.Add("strOperacao", "A") ' Alterar
            btnAlterar.Enabled = True
            btnExcluir.Enabled = True
            Session.Add("strOperacao", "C") ' Consultar
 
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodigoDaHolding", "")
        Session.Add("strCodigoDaCia", "")
        Session.Add("strCodSplit", "")

        Response.Redirect("SplitData.aspx")

    End Sub


    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodSplit", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))

        Response.Redirect("SplitData.aspx")

    End Sub


    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodSplit", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))

        Response.Redirect("SplitData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub


    Protected Sub BtnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConsultar.Click

        Session.Add("strOperacao", "C") ' Consultar

        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodSplit", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))


        Response.Redirect("SplitData.aspx")

    End Sub

    Protected Sub btnPesquisar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPesquisar.Click

        CarregaGVW()

    End Sub
End Class
